package com.tmall.wireless.virtualviewdemo.utils.imageloader.core.imageaware;

import com.tmall.wireless.virtualviewdemo.utils.imageloader.core.assist.ViewScaleType;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.media.image.PixelMap;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

public abstract class ViewAware implements ImageAware {

    protected Reference<Component> viewRef;

    protected boolean checkActualViewSize;


    public ViewAware(Component view) {
        this(view, true);
    }


    public ViewAware(Component view, boolean checkActualViewSize) {
        if (view == null) throw new IllegalArgumentException("view must not be null");

        this.viewRef = new WeakReference<Component>(view);
        this.checkActualViewSize = checkActualViewSize;
    }

    @Override
    public Component getWrappedView() {

        return viewRef.get();
    }

    @Override
    public ViewScaleType getScaleType() {
        return ViewScaleType.CROP;
    }

    @Override
    public int getId() {
        Component view = viewRef.get();
        return view == null ? super.hashCode() : view.hashCode();
    }

    @Override
    public int getWidth() {
        Component view = viewRef.get();
        if (view != null) {
            final ComponentContainer.LayoutConfig layoutConfig = view.getLayoutConfig();
            int width = 0;
            if (checkActualViewSize && layoutConfig != null && layoutConfig.width != ComponentContainer.LayoutConfig.MATCH_CONTENT) {
                width = view.getWidth(); // Get actual image width
            }
            if (width <= 0 && layoutConfig != null) width = layoutConfig.width; // Get layout width parameter
            return width;
        }
        return 0;
    }

    @Override
    public int getHeight() {
        Component view = viewRef.get();
        if (view != null) {
            final ComponentContainer.LayoutConfig layoutConfig = view.getLayoutConfig();
            int height = 0;
            if (checkActualViewSize && layoutConfig != null && layoutConfig.height != ComponentContainer.LayoutConfig.MATCH_CONTENT) {
                height = view.getHeight(); // Get actual image height
            }
            if (height <= 0 && layoutConfig != null) height = layoutConfig.height; // Get layout height parameter
            return height;
        }
        return 0;
    }

    @Override
    public boolean isCollected() {
        return viewRef.get() == null;
    }

    @Override
    public boolean setImageElement(Element element) {
        Component component = viewRef.get();
        if (component == null) {
            return false;
        }
        ((Image) component).setImageElement(element);
        return true;
    }

    @Override
    public boolean setImagePixelMap(PixelMap pixelMap) {
        Component component = viewRef.get();
        if (component == null) {
            return false;
        }
        ((Image) component).setPixelMap(pixelMap);
        return true;
    }

    /**
     * Should set drawable into incoming view. Incoming view is guaranteed not null.<br />
     * This method is called on UI thread.
     *
     *
     * @param element
     * @param view
     */
    protected abstract void setImageElementInto(Element element, Component view);


    protected abstract void setImagePixelMapInto(PixelMap pixelMap, Component view);


}
